package com.fr.web.core.service;

import com.fr.base.FRContext;
import com.fr.base.StringUtils;
import com.fr.base.core.ComparatorUtils;
import com.fr.base.xml.BaseXMLUtils;
import com.fr.report.PageSet;
import com.fr.report.ResultWorkBook;
import com.fr.report.core.cal.LayerPageReport;
import com.fr.report.core.cal.LayerReport;
import com.fr.report.io.AppExporter;
import com.fr.report.io.CSVExporter;
import com.fr.report.io.ExcelExporter;
import com.fr.report.io.LargeDataPageExcelExporter;
import com.fr.report.io.PDFExporter;
import com.fr.report.io.PDFExporter2;
import com.fr.report.io.PageExcelExporter;
import com.fr.report.io.PageToSheetExcelExporter;
import com.fr.report.io.SVGExporter;
import com.fr.report.io.TextExporter;
import com.fr.report.io.WordExporter;
import com.fr.schedule.ViewScheduleSavedInfo;
import com.fr.schedule.util.ScheduleUtils;
import com.fr.util.Utils;
import com.fr.web.Browser;
import com.fr.web.ErrorHandler;
import com.fr.web.core.ErrorHandlerHelper;
import com.fr.web.core.SessionIDInfor;
import com.fr.web.core.WebUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ExportService extends NoOPService
{
  private static ExportService EXPORT_SERVICE = new ExportService();

  public static ExportService getInstance()
  {
    return EXPORT_SERVICE;
  }

  public boolean accept(String paramString)
  {
    return paramString.equals("export");
  }

  public void process(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse, String paramString)
    throws Exception
  {
    dealWithExport(paramHttpServletRequest, paramHttpServletResponse, paramString, false);
  }

  public static void dealScheduleExport(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    String str1 = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "format");
    if (StringUtils.isBlank(str1))
      return;
    Map localMap1 = WebUtils.createSessionIDParameterMap(paramHttpServletRequest);
    File localFile1 = ScheduleUtils.getScheduleSavaPathFile(localMap1);
    Map localMap2 = ScheduleUtils.getParaMap(localMap1);
    if ((localFile1.exists()) && (localFile1.isDirectory()))
    {
      File localFile2 = new File(localFile1, "info.xml");
      if (!(localFile2.exists()))
        return;
      ViewScheduleSavedInfo localViewScheduleSavedInfo = new ViewScheduleSavedInfo();
      try
      {
        localViewScheduleSavedInfo = (ViewScheduleSavedInfo)BaseXMLUtils.readXMLFile(new FileInputStream(localFile2), localViewScheduleSavedInfo);
      }
      catch (Exception localException)
      {
        localException.printStackTrace();
      }
      String str2 = null;
      if (StringUtils.isNotEmpty(WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "fileName")))
        str2 = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "fileName");
      paramHttpServletResponse.reset();
      paramHttpServletResponse.setHeader("Cache-Control", "public");
      paramHttpServletResponse.setHeader("Cache-Control", "max-age=3");
      if (str1.equalsIgnoreCase("excel"))
      {
        str2 = (StringUtils.isEmpty(str2)) ? localViewScheduleSavedInfo.getExcelName(localMap2) : str2;
        paramHttpServletResponse.setContentType("application/x-excel");
        paramHttpServletResponse.setHeader("extension", "xls");
        paramHttpServletResponse.setHeader("Content-disposition", "attachment; filename=download.xls");
      }
      else if (str1.equalsIgnoreCase("word"))
      {
        str2 = (StringUtils.isEmpty(str2)) ? localViewScheduleSavedInfo.getWordName(localMap2) : str2;
        paramHttpServletResponse.setContentType("application/msword");
        paramHttpServletResponse.setHeader("extension", "doc");
        paramHttpServletResponse.setHeader("Content-disposition", "attachment; filename=download.doc");
      }
      else if (str1.equalsIgnoreCase("pdf"))
      {
        str2 = (StringUtils.isEmpty(str2)) ? localViewScheduleSavedInfo.getPDFName(localMap2) : str2;
        paramHttpServletResponse.setContentType("application/pdf");
        paramHttpServletResponse.setHeader("extension", "pdf");
        paramHttpServletResponse.setHeader("Content-disposition", "attachment; filename=download.pdf");
      }
      else
      {
        return;
      }
      File localFile3 = new File(localFile1, str2);
      if (localFile3.exists())
      {
        ServletOutputStream localServletOutputStream = paramHttpServletResponse.getOutputStream();
        localServletOutputStream.write(Utils.inputStream2Bytes(new FileInputStream(localFile3)));
        localServletOutputStream.flush();
        localServletOutputStream.close();
      }
    }
  }

  public static void dealWithExport(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse, String paramString, boolean paramBoolean)
    throws Exception
  {
    Object localObject1;
    Object localObject3;
    if (paramString == null)
      paramString = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "sessionID");
    SessionIDInfor localSessionIDInfor = SessionDealWith.getSessionIDInfor(paramString);
    if (localSessionIDInfor == null)
      return;
    String str1 = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "format");
    paramHttpServletResponse.reset();
    String str2 = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "__filename__");
    if (str2 == null)
      str2 = localSessionIDInfor.getReportletTitle().replaceAll("\\s", "_");
    Browser localBrowser = Browser.resolve(paramHttpServletRequest);
    str2 = localBrowser.getEncodedFileName4Download(str2);
    paramHttpServletResponse.setHeader("Cache-Control", "public");
    paramHttpServletResponse.setHeader("Cache-Control", "max-age=3");
    if (str1.equalsIgnoreCase("PDF"))
    {
      paramHttpServletResponse.setContentType("application/pdf");
      paramHttpServletResponse.setHeader("extension", "pdf");
      if (paramBoolean)
        paramHttpServletResponse.setHeader("Content-disposition", "anyword; filename=" + str2 + ".pdf");
      else
        paramHttpServletResponse.setHeader("Content-disposition", "attachment; filename=" + str2 + ".pdf");
      localObject2 = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "extype");
      boolean bool1 = ComparatorUtils.equals(WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "isPDFPrint"), "true");
      localSessionIDInfor.setAttribute("genpdfprint", new Boolean(bool1));
      if ("rs".equals(localObject2))
        localObject1 = new PDFExporter2(bool1);
      else
        localObject1 = new PDFExporter(bool1);
    }
    else if (str1.equalsIgnoreCase("Excel"))
    {
      localObject2 = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "extype");
      localObject3 = localSessionIDInfor.getWorkBook2Show();
      boolean bool2 = true;
      for (int i = 0; i < ((ResultWorkBook)localObject3).getReportCount(); ++i)
        if ((((ResultWorkBook)localObject3).getReport(i) instanceof LayerReport) || (((ResultWorkBook)localObject3).getReport(i) instanceof LayerPageReport))
        {
          if ("simple".equals(localObject2))
            bool2 = false;
          if ("sheet".equals(localObject2))
            break;
          localObject2 = "ldpage";
          break;
        }
      if ("ldpage".equals(localObject2))
      {
        if (localBrowser.shouldSetContentTypeOnZipDownload())
          paramHttpServletResponse.setContentType("application/zip");
        paramHttpServletResponse.setHeader("extension", "zip");
        paramHttpServletResponse.setHeader("Content-disposition", "attachment; filename=" + str2 + ".zip");
        localObject1 = new LargeDataPageExcelExporter(localSessionIDInfor.getPaperSettingList(), bool2);
      }
      else
      {
        paramHttpServletResponse.setContentType("application/x-excel");
        paramHttpServletResponse.setHeader("extension", "xls");
        paramHttpServletResponse.setHeader("Content-disposition", "attachment; filename=" + str2 + ".xls");
        if ("simple".equals(localObject2))
          localObject1 = new ExcelExporter(localSessionIDInfor.getPaperSettingList());
        else if ("sheet".equals(localObject2))
          localObject1 = new PageToSheetExcelExporter(localSessionIDInfor.getPaperSettingList());
        else
          localObject1 = new PageExcelExporter(localSessionIDInfor.getPaperSettingList());
      }
    }
    else if (str1.equalsIgnoreCase("Word"))
    {
      paramHttpServletResponse.setContentType("application/msword");
      paramHttpServletResponse.setHeader("extension", "doc");
      paramHttpServletResponse.setHeader("Content-disposition", "attachment; filename=" + str2 + ".doc");
      localObject1 = new WordExporter();
    }
    else if (str1.equalsIgnoreCase("SVG"))
    {
      paramHttpServletResponse.setContentType("image/svg+xml");
      paramHttpServletResponse.setHeader("extension", "svg");
      paramHttpServletResponse.setHeader("Content-disposition", "attachment; filename=" + str2 + ".svg");
      localObject1 = new SVGExporter();
    }
    else if (str1.equalsIgnoreCase("CSV"))
    {
      paramHttpServletResponse.setContentType("application/octet-stream");
      paramHttpServletResponse.setHeader("extension", "csv");
      paramHttpServletResponse.setHeader("Content-disposition", "attachment; filename=" + str2 + ".csv");
      localObject1 = new CSVExporter();
    }
    else if (str1.equalsIgnoreCase("Text"))
    {
      paramHttpServletResponse.setContentType("application/octet-stream");
      paramHttpServletResponse.setHeader("extension", "txt");
      paramHttpServletResponse.setHeader("Content-disposition", "attachment; filename=" + str2 + ".txt");
      localObject1 = new TextExporter();
    }
    else
    {
      ErrorHandlerHelper.getErrorHandler().error(paramHttpServletRequest, paramHttpServletResponse, "Cannot recognize the specifed export format:" + str1);
      return;
    }
    Object localObject2 = paramHttpServletResponse.getOutputStream();
    try
    {
      if (localObject1 instanceof ExcelExporter)
      {
        ((ExcelExporter)localObject1).export((OutputStream)localObject2, localSessionIDInfor.getWorkBook2Show(), true);
      }
      else
      {
        localObject3 = localSessionIDInfor.getPrintPreviewPageSet4Traversing();
        ((AppExporter)localObject1).export((OutputStream)localObject2, (PageSet)localObject3);
      }
    }
    catch (Exception localException)
    {
      FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
      ErrorHandlerHelper.getErrorHandler().error(paramHttpServletRequest, paramHttpServletResponse, localException);
    }
    try
    {
      ((OutputStream)localObject2).flush();
      ((OutputStream)localObject2).close();
    }
    catch (IOException localIOException)
    {
    }
    localSessionIDInfor.setAttribute("genpdfprint", new Boolean(false));
  }
}